<?php
/**
 * Created by PhpStorm.
 * User: zhangjincheng
 * Date: 17-8-15
 * Time: 上午10:52
 */

namespace WsdServer\Components\AMQPTaskSystem;

use Server\Components\AMQPTaskSystem\AMQPTaskProcess;

class WsdAMQPTaskProcess extends AMQPTaskProcess
{
    private $consume_queue;
    private $handler_class;

    public function setConsumeQueue($queue){
        $this->consume_queue = $queue;
    }

    public function setHandlerClass($class){
        $this->handler_class = $class;
    }

    public function start($process)
    {
        parent::start($process);
        $this->createDirectConsumeWsd($this->consume_queue, $this->consume_queue, $this->consume_queue,
                                        $this->consume_queue, 2, false);
    }

    protected function createDirectConsumeWsd($queue, $exchange, $routing_key, $consumerTag,
                                              $prefetch_count = 2, $global = false)
    {
        $this->channel->queue_declare($queue, false, true, false, false);
        $this->channel->exchange_declare($exchange, 'direct', false, true, false);
        $this->channel->queue_bind($queue, $exchange, $routing_key);
        $this->channel->basic_qos(0, $prefetch_count, $global);
        $this->channel->basic_consume($queue, $consumerTag, false, false, false, false, [$this, 'process_message']);
    }

    /**
     * 路由消息返回class名称
     * @param $body
     * @return string
     */
    protected function route($body)
    {
        //return WsdAMQPTask::class;
        return $this->handler_class;
    }

    protected function onShutDown(){
        //secho("Process:$this->worker_id", get_class($this) . " ... onShutDown()");
        return;
    }
}